{{extend "resource_layout.html"}}

{{block content_main}}
{{use "jquery"}}
{{use "jqeasyui"}}
{{from uliweb.core.js import json_dumps}}
{{use "facebox"}}
{{use "jqutils", ajaxForm=True}}
<div class="content">
    <div class="box">
        <h2>人员调整</h2>
        <div class="box-body">
            <label for="field_system">请选择系统：</label>
            {{<<system}}
            <div class="group">
                <p class="description">当前系统人员：</p>
                <table id="resource"></table>
            </div>
            <div class="group">
                <p class="description">备选人员清单：</p>
                <div class="query">
                {{embed query_form}}
                </div>
                <table id="human"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
var add_to_resource = function(_ids){
    //检查系统是否选中
    if (!$('#system').val()){
        $.messager.alert('出错', '请先选择对应系统!', 'error');
    }else{
        $.ajax({
          type: 'POST',
          url: "/resource/usage/add",
          dataType: 'json',
          data: ({'ids':_ids, system:$('#system').val()}),
          success: function(data){
            if (data.success){
              show_message(data.data, 'success');
              $('#human').datagrid('clearSelections');
              $('#resource').datagrid('reload');
              $('#human').datagrid('reload');
            }else{
              show_message(data.data, 'error');
            }
          }
        });
    }
}

var remove_from_resource = function(ids){
    //检查系统是否选中
    $.ajax({
      type: 'POST',
      url: "/resource/usage/remove",
      data: ({'ids':ids}),
      dataType: 'json',
      success: function(data){
        if (data.success){
          show_message(data.data, 'success');
          $('#resource').datagrid('clearSelections');
          $('#resource').datagrid('reload');
        }else{
          show_message(data.data, 'error');
        }
      }
    });
}

$(function(){
    $.facebox.settings.loadingImage = '{{=url_for_static('facebox/loading.gif')}}'
    $.facebox.settings.closeImage = '{{=url_for_static('facebox/closelabel.png')}}'

    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
    
    /**********************************************
     * 处理查询点击
     **********************************************/
    var form = $('div.query form')
    form.submit(function(){
        $('#human').datagrid('load', form.serializeObject());
        $('#human').datagrid('clearSelections');
        return false;
    });
    
    /**********************************************
     * 处理系统切换
     **********************************************/
    $(function(){
        $('#system').change(function(){
            $('#resource').datagrid('load', {'system':$(this).val()});
            $('#resource').datagrid('clearSelections');
        });
    });
    
    /**********************************************
     * 定义资源信息
     **********************************************/
    var post_editusage = function(r){
        if (r.success){
            show_message(r.message);
            jQuery(document).trigger('close.facebox');
            $("#resource").datagrid('reload');
            
        } else if (!r.success){
            $('div.error-div').remove();
            show_message(r.message, 'error');
            $.each(r.data, function(key, value){
                var f, t, e, d;
                f = 'field_' + key;
                t = $('label[for='+f+']');
                e = $('<div class="ui-state-error error-div" style="border:0;"><span class="ui-icon ui-icon-info" style="float:left;"></span><span class="ui-state-error-text">' + value + '</span></div>');
                d = t.position();
                t.after(e);
            });
        }
    }
    
    $('#resource').datagrid({
        pagination:true,
        striped: true,
        rownumbers:true,
        nowrap: false,
        width:'auto',
        height:310,
        singleSelect:true,
        url:"/resource/usage/resource",
        idField:'id',
        columns:{{=json_dumps(resource_table.get_columns())}},
        toolbar:[{
            text:'从当前系统中删除',
            iconCls:'icon-remove',
            handler:function(){
                var ids = [];
                var rows = $('#resource').datagrid('getSelections');
                for(var i=0;i<rows.length;i++){
                	ids.push(rows[i].id);
                }
                remove_from_resource(ids);
            }
        },
        {
            text:'修改信息',
            iconCls:'icon-edit',
            handler:function(){
                var row = $('#resource').datagrid('getSelected');
                var url = '/resource/usage/resource/edit/' + row.id;
                $.facebox(function() {
                    $.get(url, function(data) {
                        $.facebox(data);
                        $('#edit_usage_form').ajaxForm(post_editusage);
                    });
                });
                $('#resource').datagrid('clearSelections');
            }
        },
        {
            text:'查看人员历史',
            iconCls:'icon-search',
            handler:function(){
                var row = $('#resource').datagrid('getSelected');
                window.open('/resource/usage/resource/view/'+row.id);
                $('#resource').datagrid('clearSelections');
            }
        }
        ]
    });
    
    /**********************************************
     * 定义备选人员表格
     **********************************************/
    $('#human').datagrid({
        pagination:true,
        striped: true,
        rownumbers:true,
        nowrap: false,
        width:'auto',
        height:310,
        url:'/resource/usage/human',
        columns:{{=json_dumps(ready_table.get_columns())}},
        idField:'id',
        toolbar:[{
            text:'添加人员到系统',
            iconCls:'icon-add',
            handler:function(){
                var ids = [];
                var rows = $('#human').datagrid('getSelections');
                for(var i=0;i<rows.length;i++){
                	ids.push(rows[i].id);
                }
                add_to_resource(ids);
            }
        }]
    });
});
</script>
{{end}}

{{block sub_menu}}
{{sub_menu(menu_items, 'resource', 'usage')}}
{{end}}

